<?php

namespace Admin\Controller;

class InfoController extends AdminController {

    public function index() {
        $where = '1=1 ';

        $keywords = I('keywords');
        if ($keywords) {
            $where .= " and title like '%" . $keywords . "%'";
            $this->assign('keywords', $keywords);
        }

        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");


        if (!empty($end_date) && !empty($start_date)) {
            if ($end_date <= $start_date) {
                $this->error('开始时间不允许小于等于结束时间');
                exit;
            } else {
                $where .= " and create_time between '$start_date 00:00:00' AND '$end_date 23:59:59'";
            }
            $this->assign("start_date", $start_date);
            $this->assign("end_date", $end_date);
        } else {
            if (!empty($start_date)) {
                $where .= " and create_time >='$start_date 00:00:00'";
                $this->assign("start_date", $start_date);
            }

            if (!empty($end_date)) {
                $where .= " and create_time <= '$end_date 23:59:59'";
                $this->assign("end_date", $end_date);
            }
        }

        $M = M('Information');

        $field = '*';

        $order = "create_time desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('_list', $list);
        $this->assign('meta_title', '头条列表');
        $this->display();
    }

    public function add() {
        $id = I("id", 0, 'intval');
        if (IS_POST) {

            $data['img_url'] = I("img_url");
            $data['title'] = I("title");
            $data['desc'] = I("desc");
            $data['sort'] = I("sort", 0);

            $data['update_time'] = date('Y-m-d H:i:s', time());
            $data['desc'] = I("desc");

            if ($id == 0) {
                $res = M('Information')->add($data);
            } else {
                $res = M('Information')->where(array('info_id' => $id))->save($data);
            }

            if ($res) {
                $this->success('操作成功', U('index'));
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Information')->where(array("info_id" => $id))->find();

            $this->assign('data', $data);
            $this->assign('meta_title', '新增/修改头条');
            $this->display();
        }
    }

    public function del() {
        $id = I('id', 0, 'intval');
        if ($id == 0) {
            $this->error('错误的ID');
        }

        $res = M('Information')->where(array('info_id' => $id))->delete();

        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

   
    //推荐
    public function recmd() {
        $id = I('id', 0, 'intval');
        $status = I('s', 0, 'intval');

        $data = [];
        $data['is_index'] = $status;

        $res = M('Information')->where(array('info_id' => $id))->save($data);

        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

}
